package StackAndQueuePackage;

import org.junit.jupiter.api.Test;

import java.util.Stack;

/**
 * @author Lzm
 * @version 1.0
 */
public class isValid_ {

  @Test
  public void isValidTest(){

  }



  public boolean isValid(String s) {
    if (s.length() % 2 != 0){
      return false;
    }

    Stack<Character> stack = new Stack<>();
    char[] chars = s.toCharArray();
    for (int i = 0 ; i < chars.length ; i++){
      if (chars[i] == '('){
        stack.push(')');
      }
      else if (chars[i] == '['){
        stack.push(']');
      }
      else if(chars[i] == '{'){
        stack.push('}');
      }
      else{
        if (stack.isEmpty() || stack.peek() != chars[i]){
          return false;
        }
        stack.pop();
      }
    }
    return true;
  }
}
